home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12958 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  94 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Date: 3 Apr 1996 09:55:39 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jue2rINNchh@keats.ugrad.cs.ubc.ca>
  8. References: <31616F63.481D@lava.weeg.uiowa.edu> <4jrvv3$ask@aimnet.aimnet.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11.  
  12. In article <4jrvv3$ask@aimnet.aimnet.com>,
  13. Jonathan S. McNeill <jmcneill@aimnet.com> wrote:
  14. >In article <31616F63.481D@lava.weeg.uiowa.edu>,
  15. >Artur Wojdat  <awojdat@lava.weeg.uiowa.edu> wrote:
  16. >
  17. >>    Is there a function or some sort of way that I could remove '\n' 
  18. >>charecter form the end of the string. I'm reading from two files, want to 
  19. >>form one line of text and then have it printed out to stdout. I use fgets to 
  20. >>read from the file and I noticed that it appends newline char at the end.
  21. >
  22. >Well, if you want ultimate control over your data, just use a loop and
  23. >get a character at a time.  Disclaimer:  the following is an example, has
  24.  
  25. That is a sound plan...
  26.  
  27. >not been tested, and I agree that there are about a million ways you could do
  28. >this.
  29. >
  30. >/*
  31. > * loop through characters until newline or end of file
  32. > */
  33. >while ( (ch = getc(stream)) != '\n' && ch != EOF ) {
  34. >
  35. >    sprintf(buffer, "%s%c", buffer, ch);
  36. >}
  37.  
  38. But the execution is severely botched up! My goodness, where did you learn your
  39. C?
  40.  
  41. Using the buffer as the source and destination? Using sprintf() to do array
  42. manipulations?
  43.  
  44. If you are going to be silly enough to use a fixed buffer for a job that may
  45. involve files containing lines of arbitrary length, at least use something
  46. like:
  47.  
  48.     buffer[i++] = ch;
  49. or    *bufptr++ = ch;
  50.  
  51. Please don't post nonsense to this newsgroup. It doesn't do anyone any good.
  52.  
  53. April Fool's day was two days ago.
  54.  
  55. >/*
  56. > * add a trailing nul character so that you can use string functions
  57. > */
  58. >sprintf(buffer, "%s\0"); 
  59.  
  60. Oh dear. This will clobber the buffer, not append to it, you silly! And
  61. sprintf() already adds a zero character by itself. The 's' in its name stands
  62. for 'string', in case you didn't know.
  63.  
  64. This problem requires no buffering beyond what is already performed in the
  65. standard IO library.
  66.  
  67.  
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <errno.h>
  71.  
  72. void concatprint(FILE *f1, FILE *f2)
  73.  
  74. /*
  75.  * read lines from f1 and f2 simultaneously, and print them side by side
  76.  * f1 and f2 must be valid streams open for reading.
  77.  */
  78.  
  79. {
  80.     int c;
  81.  
  82.     do {
  83.         if (!feof(f1) && !ferror(f1)) 
  84.             while ((c = getc(f1)) != '\n' && c != EOF)
  85.                 putchar(c);
  86.         if (!feof(f2) && !ferror(f2)) 
  87.             while ((c = getc(f2)) != '\n' && c != EOF)
  88.                 putchar(c);
  89.         putchar('\n');
  90.     } while ((!feof(f1) && !ferror(f1)) || (!feof(f2) && !ferror(f2)));
  91. }
  92. -- 
  93.  
  94.